package com.haste.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.haste.common.domain.po.BaseMenuPo;
import com.haste.common.domain.po.BaseElementPo;
import com.haste.common.service.IBaseMenuService;
import com.haste.common.service.IBaseElementService;
import com.haste.system.dao.MenuDao;
import com.haste.system.domain.vo.MenuVo;
import com.haste.system.service.IMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author haste
 */
@Service
public class MenuServiceImpl implements IMenuService {
    @Resource
    private IBaseMenuService baseMenuService;

    @Resource
    private MenuDao menuDao;

    @Resource
    private IBaseElementService baseUserMenusService;

    @Override
    public List<MenuVo> list() {
        String userId = "2";
        return menuDao.list(userId);
    }

    @Override
    public void save(MenuVo menuVo) {
        BaseMenuPo baseMenuPo = this.menuVo2MenuPo(menuVo);
        baseMenuService.save(baseMenuPo);

        BaseElementPo baseUserConfigPo = this.menuVo2UserConfigPo(menuVo,baseMenuPo);
        baseUserMenusService.save(baseUserConfigPo);
    }

    @Override
    public void updateById(MenuVo menuVo) {
        BaseMenuPo baseMenuPo = this.menuVo2MenuPo(menuVo);
        baseMenuService.updateById(baseMenuPo);

        BaseElementPo baseUserConfigPo = this.menuVo2UserConfigPo(menuVo, baseMenuPo);
        LambdaQueryWrapper<BaseElementPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseElementPo::getElementId,baseUserConfigPo.getElementId());
        queryWrapper.eq(BaseElementPo::getUserId,baseUserConfigPo.getUserId());
        List<BaseElementPo> list = baseUserMenusService.list(queryWrapper);
        if (CollUtil.isEmpty(list)){
            baseUserMenusService.save(baseUserConfigPo);
        }
        else{
            LambdaUpdateWrapper<BaseElementPo> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(BaseElementPo::getPositionX,menuVo.getPositionX());
            updateWrapper.set(BaseElementPo::getPositionY,menuVo.getPositionY());
            updateWrapper.eq(BaseElementPo::getUserId,baseUserConfigPo.getUserId());
            updateWrapper.eq(BaseElementPo::getElementId,baseUserConfigPo.getElementId());
            baseUserMenusService.update(updateWrapper);
        }
    }

    @Override
    public void tidyUp() {
        menuDao.tidyUp();
    }

    @Override
    public void removeById(String id) {
        String userId = "2";
        menuDao.removeById(id,userId);
    }

    private BaseElementPo menuVo2UserConfigPo(MenuVo menuVo, BaseMenuPo baseMenuPo) {
        String userId = "2";
        BaseElementPo baseUserConfigPo = new BaseElementPo();
        baseUserConfigPo.setElementId(baseMenuPo.getId());
        baseUserConfigPo.setUserId(userId);
        baseUserConfigPo.setPositionX(menuVo.getPositionX());
        baseUserConfigPo.setPositionY(menuVo.getPositionY());
        return baseUserConfigPo;
    }

    private BaseMenuPo menuVo2MenuPo(MenuVo menuVo) {
        BaseMenuPo baseMenuPo = new BaseMenuPo();
        BeanUtil.copyProperties(menuVo,baseMenuPo);
        return baseMenuPo;
    }
}